/**
 * 个人信息变更管理
 * 
 * @version 1.0.0
 * @author yangyibao
 */

var userTemp = $("#userFormTemp").html().trim();
Mustache.parse(userTemp);//预编译
$(function () {
	loadSelfInf();
	$("#userPhotoSubmitBtn").click(function(){
		var bc = $("#fileName").val().replace(/\\/g,"/");
		$("#fileName").val(bc);
		$.post("../user/uploadPhotoFtp",{fileName:bc},function(data){
			if(data.flg){
	       		 succ();
		         $("#img_user").attr("src",getFtpUrl+bc);
	       	 }
		});
	});
});
function loadSelfInf(){
	$.post("../user/selfInfoDetail",function(userData){
		$("#tab_1_1").html(Mustache.render(userTemp, userData));
		$("#userForm").validate({
			  errorElement: 'span',
			  errorClass:'help-inline',
			  focusInvalid: false,ignore: '',
			  rules: {userName: {required: true,pattern:/^[\u4e00-\u9fa5]{2,10}$/},userTel:{required: true,mobileCN:true}
			  ,userEmail:{email:true}},
			  messages:{userName:{pattern:'真实用户名格式非法，必须是中文且2-10个汉字!'}},
			  errorPlacement: function(error,element){error.insertAfter(element);},
			  highlight: function(element) {
				  $(element).closest('.control-group').removeClass('success');
				  $(element).closest('.help-inline').removeClass('valid');
				  $(element).closest('.help-inline').removeClass('ok');
				  $(element).closest('.control-group').addClass('error');
				  
			  },
			  unhighlight: function(element) {
				  $(element).closest('.control-group').removeClass('error');
				  },
			  success: function(label) {
				  label.closest('.control-group').removeClass('error');
				  label.closest('.control-group').addClass('success');
				  label.closest('.help-inline').addClass('valid ok');
				  },
			  submitHandler:function(form){
				  $.post('../user/updateUserExtInfo',$("#userForm").serialize(),function(data){if(data.flg){succ();reset()}else{error(data.errorMsg);}});
			      return false;
			  }});
		$("#userGender option[value='"+userData.userProfile.userGender+"']").attr("selected",true);
		$("#submitBtn").click(function(){
//			alert($("#userEmail").val());
//			if($("#userEmail").val()==""||$("#userEmail").val()==null){
//				$("#userEmail").val(" ");
//			}
			$("#userForm").submit();});
		
	});
}
function reset(){
	document.getElementById("userForm").reset();
	loadSelfInf();
	$(".help-block").closest('.form-group').removeClass('has-error');
    $(".help-block").html('');
}
//上传图片
var uploadImgFn = function ($this) {
	  // var id=$($this).parent().data("id");
	 
	$("#fileuploadBtn").click();
};
function imgSubBtn(){
	if($("#userPhoto").val()!=null||$("#userPhoto").val()!=''){
		$("#userPhotoSubmitBtn").attr("disabled",false);
	}else{
		$("#userPhotoSubmitBtn").attr("disabled",true);
	}
}

var ffwangFn = function(){
	return {
		getImgPathFtpFn: function (xhr) {
		    if (xhr.readyState == 1) {
		        console.log("这里写一些调用开始的东西。")
		        swal("Wait。。。","图片正在上传。。。请勿操作！！","");
		    }
		    if (xhr.readyState == 4 && xhr.status == 200) {
		        var data = JSON.parse(xhr.responseText);
		        // vm.data.returnData[flag_].projectInfo.productType.im
		        console.log(JSON.stringify(data));
		        swal("成功","图片上传成功","success");
		        $("#fileView").prepend('<div class="file-image-upload" data-path="' + data.file.path + '" data-add="fileView"><img id="imgSrc"  src="'+getFtpUrl + data.file.path + '"></div>')
		        var a = $("#imgSrc").prop("src")
		        $("#fileView").prop("src",a)
		        $("#fileName").val(a.split("/")[3]+'\\'+a.split("/")[4]+'\\'+a.split("/")[5])
		        $("#add").hide();
		    }
		},
		deleteImgFn:function(this_){
            $(this_).parent(".file-image-upload").remove();
            //$("#add").show()
            $("#imgContent").html('<input type="hidden" id="userPhoto" name="userPhoto">');
            $("#imgContent").append('<img id="add" src="../resources/imgs/icon/upload.png" onclick="uploadImgFn(this)"style="width:40px;height:40px">')
            $("#fileuploadBtn").remove();
            $("#addEnterpriseInfoVOForm").append('<input type="file" id="fileuploadBtn" style="display:none" onchange="filetoftp(&apos;file&apos;)" multiple="multiple" name="file">')
        },
	}
}()
//上传按钮触发(单文件和多文件公用)
function filetoftp(file){
	//标签名---参数file为空时 fileName取默认值 “file”  否则取 file的值
	var fileName = file||"file";
	//路径
	var path = "img";
	sendFileToFtp(fileName,path, true, ffwangFn.getImgPathFtpFn)

}